package com.general.framework.wukong.blockingQueue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class ArrayBlockingQueueDemo{
    public static void main(String[] args) {
        BlockingQueue queue  = new ArrayBlockingQueue(3);
        Interviewer interviewer = new Interviewer(queue);
        Interviewee interviewee = new Interviewee(queue);
        new Thread(interviewer).start();
        new Thread(interviewee).start();
    }

}

class Interviewer implements Runnable{
    BlockingQueue<String> queue;
    public Interviewer(BlockingQueue<String> queue){
        this.queue = queue;
    }
    @Override
    public void run() {
        for (int i = 1; i <=10 ; i++) {
            String candidate = "Candidate" + i;
            try {
                queue.put(candidate);
                System.out.println("已安排好了第 " + candidate+" 位候选者");
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }
        try {
            queue.put("Stop");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}

class Interviewee implements Runnable{
    BlockingQueue<String> queue;
    public Interviewee(BlockingQueue<String> queue){
        this.queue = queue;
    }
    @Override
    public void run() {
        while (true){
            try {
                Thread.sleep(1000);
                String candidate = queue.take();
                if ("Stop".equals(candidate)){
                    break;
                }
                System.out.println("第 " + candidate+" 位候选者面试完毕");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}